/*
Problem:    脱险
Assemble:   NOIP_moni_8
Anthor:     Alchemist
Date:       2011.10.21
Tag:        Search, Graph
Difficulty: ****
Description:
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 13, maxt = 51;
int r,c,t;
int adj[maxn * maxn][maxn * 4 * maxt] = {0};
char m[maxn][maxn] = {0};
int mp[maxn][maxn] = {0},p = 0,o = 0;

const int dir[4][2] = {0,1,1,0,-1,0,0,-1};
bool vis[maxn][maxn] = {0};

void bfs(int sx,int sy)
{
    int x,y;
    queue<pair<int,int> > q;
    queue<int> qs;
    q.push(make_pair(sx,sy));
    qs.push(0);
    while(!q.empty())
    {
        pair<int, int> pii = q.front();
        x = pii.first;
        y = pii.second;
        if(m[x][y] == 'P' && qs.front() <= t)
            adj[mp[x][y]][qs.front() * (o + 1) + mp[sx][sy]] = 1;
        for(int i = 0; i < 4; i ++)
            if(!vis[x + dir[i][0]][y + dir[i][1]] && m[x + dir[i][0]][y + dir[i][1]] != '*' && m[x + dir[i][0]][y + dir[i][1]] != 'O')
            {
                vis[x + dir[i][0]][y + dir[i][1]] = 1;
                q.push(make_pair(x + dir[i][0], y + dir[i][1]));
                qs.push(qs.front() + 1);
            }
        q.pop();
        qs.pop();
    }

}

int match;
int mat[maxn * maxn];
bool used[maxn * maxn];

bool crosspath(int k)
{
	for (int i = 1; i <= (o + 1) * (t + 1); i ++)
	{
		if (adj[k][i] && !used[i])
		{
			used[i] = true;
			if (mat[i] == 0 || crosspath(mat[i]))
			{
				mat[i] = k;
				return true;
			}
		}
	}
	return false;
}

void hungary()
{
	match = 0;
	for (int i = 1; i <= p; i ++)
	{
		memset(used,0,sizeof(used));
		if (crosspath(i))
			match ++;
	}
}

int main()
{
    freopen("escape.in","r",stdin);
    freopen("escape.out","w",stdout);
    int i,j,k;
    cin >> r >> c >> t;
    for(i = 0; i <= r + 1; i ++)
        m[i][0] = m[i][c+1] = '*';
    for(j = 0; j <= c + 1; j ++)
        m[0][j] = m[r+1][j] = '*';

    for(i = 1; i <= r; i ++)
    for(j = 1; j <= c; j ++)
        cin >> m[i][j];
    for(i = 1; i <= r; i ++)
    for(j = 1; j <= c; j ++)
    {
        if(m[i][j] == 'P')
        {
            mp[i][j] = ++ p;
        }
        else if(m[i][j] == 'O')
        {
            mp[i][j] = ++ o;
        }
    }
    for(i = 1; i <= r; i ++)
    {
        memset(vis,0,sizeof(vis));
        if(m[i][c] == 'O')
            bfs(i,c);
        memset(vis,0,sizeof(vis));
        if(m[i][1] == 'O')
            bfs(i,1);
    }

    for(j = 1; j <= c; j ++)
    {
        memset(vis,0,sizeof(vis));
        if(m[r][j] == 'O')
            bfs(r,j);
        memset(vis,0,sizeof(vis));
        if(m[1][j] == 'O')
            bfs(1,j);
    }
    hungary();
    cout << match << endl;
    return 0;
}
